//The MIT License
//
//Copyright (c) 2009 nodchip
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
package tv.dyndns.kishibe.client.setting;

import tv.dyndns.kishibe.client.UserData;
import tv.dyndns.kishibe.client.util.SerialTimer;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class PanelSettingOther extends VerticalPanel implements ClickHandler {
	private static final String GROUP_SE = "SE";
	private static final String GROUP_RANKING_MOVE = "RANKING_MOVE";
	private static final String GROUP_TIMER = "TIMER";
	private final RadioButton radioButtonSeOn = new RadioButton(GROUP_SE, "オン");
	private final RadioButton radioButtonSeOff = new RadioButton(GROUP_SE, "オフ");
	private final RadioButton radioButtonRankingMoveOn = new RadioButton(GROUP_RANKING_MOVE, "オン");
	private final RadioButton radioButtonRankingMoveOff = new RadioButton(GROUP_RANKING_MOVE, "オフ");
	private final RadioButton radioButtonTimerAuto = new RadioButton(GROUP_TIMER, "自動切換え(デフォルト)");
	private final RadioButton radioButtonTimerNoDelay = new RadioButton(GROUP_TIMER, "遅延無しモード(高性能PC向け)");
	private final RadioButton radioButtonTimerCooperation = new RadioButton(GROUP_TIMER, "協調動作モード(低性能PC向け)");

	public PanelSettingOther() {
		setHorizontalAlignment(ALIGN_CENTER);

		// サウンド
		{
			if (UserData.getInstance().getPlaySound()) {
				radioButtonSeOn.setValue(true);
			} else {
				radioButtonSeOff.setValue(true);
			}

			radioButtonSeOn.addClickHandler(this);
			radioButtonSeOff.addClickHandler(this);

			final VerticalPanel panel = new VerticalPanel();
			panel.add(new HTML("▼効果音のオン／オフ"));
			panel.add(radioButtonSeOn);
			panel.add(radioButtonSeOff);
			add(panel);
		}

		// ランキング変動
		{
			if (UserData.getInstance().getRankingMove()) {
				radioButtonRankingMoveOn.setValue(true);
			} else {
				radioButtonRankingMoveOff.setValue(true);
			}

			radioButtonRankingMoveOn.addClickHandler(this);
			radioButtonRankingMoveOff.addClickHandler(this);

			final VerticalPanel panel = new VerticalPanel();
			panel.add(new HTML("▼ゲーム中のランキング上下変動表示"));
			panel.add(radioButtonRankingMoveOn);
			panel.add(radioButtonRankingMoveOff);
			add(panel);
		}

		// タイマーモード
		{
			switch (UserData.getInstance().getTimerMode()) {
			case SerialTimer.MODE_AUTO:
				radioButtonTimerAuto.setValue(true);
				break;
			case SerialTimer.MODE_NO_DELAY:
				radioButtonTimerNoDelay.setValue(true);
				break;
			case SerialTimer.MODE_COOPERATION:
				radioButtonTimerCooperation.setValue(true);
				break;
			}

			radioButtonTimerAuto.addClickHandler(this);
			radioButtonTimerNoDelay.addClickHandler(this);
			radioButtonTimerCooperation.addClickHandler(this);

			final VerticalPanel panel = new VerticalPanel();
			panel.add(new HTML("▼タイマーの動作モード"));
			panel.add(radioButtonTimerAuto);
			panel.add(radioButtonTimerNoDelay);
			panel.add(radioButtonTimerCooperation);
			add(panel);
		}
	}

	@Override
	public void onClick(ClickEvent event) {
		final Widget sender = (Widget) event.getSource();
		if (sender == radioButtonSeOn) {
			UserData.getInstance().setPlaySound(true);

		} else if (sender == radioButtonSeOff) {
			UserData.getInstance().setPlaySound(false);

		} else if (sender == radioButtonRankingMoveOn) {
			UserData.getInstance().setRankingMove(true);

		} else if (sender == radioButtonRankingMoveOff) {
			UserData.getInstance().setRankingMove(false);

		} else if (sender == radioButtonTimerAuto) {
			UserData.getInstance().setTimerMode(SerialTimer.MODE_AUTO);
			SerialTimer.setMode(SerialTimer.MODE_AUTO);

		} else if (sender == radioButtonTimerNoDelay) {
			UserData.getInstance().setTimerMode(SerialTimer.MODE_NO_DELAY);
			SerialTimer.setMode(SerialTimer.MODE_NO_DELAY);

		} else if (sender == radioButtonTimerCooperation) {
			UserData.getInstance().setTimerMode(SerialTimer.MODE_COOPERATION);
			SerialTimer.setMode(SerialTimer.MODE_COOPERATION);
		}
	}
}
